/**
 * 通过画不通颜色的园来实现
 * 享元模式
 */

class Circle {
    constructor(color){
       this.color = color;    
    }
    setX(x) {
       this.x = x;
    }
    setY(y) {
       this.y = y;
    }
    setRadius(radius) {
       this.radius = radius;
    }
    draw() {
       console.log("Circle: Draw() [Color : " + this.color
          +", x : " + this.x +", y :" + this.y +", radius :" + this.radius+']');
    }
}

class ShapeFactory{
    constructor(){
        this.map = new Map()
    }
    getCircle(color){
        let circle = this.map.get(color)
        if(!circle){
            circle = new Circle(color)
            this.map.set(color,circle)
            console.log('创建了新对象')
        }
        return circle
    }
}

//测试
const factory = new ShapeFactory()
const colors =["Red", "Green", "Blue", "White", "Black" ];
for(let i=0; i < 20; ++i) {
    const circle = factory.getCircle(
        colors[Math.floor(Math.random()*colors.length)]
    );
    circle.setX(Math.floor(Math.random()*100));
    circle.setY(Math.floor(Math.random()*100));
    circle.setRadius(100);
    circle.draw();
}


 